package conchmusic.conchmusicclient.util;

import android.content.res.Resources;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Pan Ke on 2017/4/20.
 */

public class Executor {

    private final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private final MediaType FILE = MediaType.parse("application/octet-stream");

    private final OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(10, TimeUnit.SECONDS)
            .readTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)
            .build();

    private String transfer(Request request) {
        String json;
        try {
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                json = response.body().string();
            } else {


                json = "{\"type\":\"invalidInfo\",\"msg\":" + "\"" + response.code() + "\"" + "}";
            }
            response.close();
        } catch (IOException e) {
            json = "{\"type\":\"invalidInfo\",\"msg\":" + "\"" + e.getMessage()+ "\"" + "}";
        }

        return json;
    }

    private String[] getConfig(Resources resources) {
        String[] configs = new String[3];
        try {
            Properties properties = new Properties();
            InputStream inputStream = resources.getAssets().open("config.properties");
            properties.load(inputStream);
            configs[0] = properties.getProperty("ip");
            configs[1] = properties.getProperty("port");
            configs[2] = properties.getProperty("projectName");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return configs;
    }

    public String registerTransfer(Resources resources, String registerJson) {

        String[] configs = new Executor().getConfig(resources);
        RequestBody requestBody = RequestBody.create(JSON, registerJson);
        Request request = new Request.Builder()
                .url("http://" + configs[0] + ":" + configs[1] + "/" + configs[2] + "/register")
                .post(requestBody)
                .build();
        return new Executor().transfer(request);
    }

    public String loginTransfer(Resources resources, String loginJson) {
        String[] configs = new Executor().getConfig(resources);
        RequestBody requestBody = RequestBody.create(JSON, loginJson);
        Request request = new Request.Builder()
                .url("http://" + configs[0] + ":" + configs[1] + "/" + configs[2] + "/login")
                .post(requestBody)
                .build();
        return new Executor().transfer(request);
    }

    public String getUserInfoTransfer(Resources resources, int userId) {
        String[] configs = new Executor().getConfig(resources);
        Request request = new Request.Builder()
                .url("http://" + configs[0] + ":" + configs[1] + "/" + configs[2] + "/getUserInfo/" + userId)
                .get()
                .build();
        return new Executor().transfer(request);
    }

    public String createSlistTransfer(Resources resources, String createSlistJson) {
        String[] configs = new Executor().getConfig(resources);
        RequestBody requestBody = RequestBody.create(JSON, createSlistJson);
        Request request = new Request.Builder()
                .url("http://" + configs[0] + ":" + configs[1] + "/" + configs[2] + "/createSlist")
                .post(requestBody)
                .build();
        return new Executor().transfer(request);
    }

    public String deleteSlistTransfer(Resources resources, int slistId) {
        String[] configs = new Executor().getConfig(resources);
        Request request = new Request.Builder()
                .url("http://" + configs[0] + ":" + configs[1] + "/" + configs[2] + "/deleteSlist/" + slistId)
                .get()
                .build();
        return new Executor().transfer(request);
    }

    public String modifySlistTransfer(Resources resources, String modifySlistJson) {
        String[] configs = new Executor().getConfig(resources);
        RequestBody requestBody = RequestBody.create(JSON, modifySlistJson);
        Request request = new Request.Builder()
                .url("http://" + configs[0] + ":" + configs[1] + "/" + configs[2] + "/modifySlist")
                .post(requestBody)
                .build();
        return new Executor().transfer(request);
    }

    public String findAllSlistTransfer(Resources resources, int userId) {
        String[] configs = new Executor().getConfig(resources);
        Request request = new Request.Builder()
                .url("http://" + configs[0] + ":" + configs[1] + "/" + configs[2] + "/findAllSlist/" + userId)
                .get()
                .build();
        return new Executor().transfer(request);
    }

    public String getComment(Resources resources, int songId) {
        String[] configs = new Executor().getConfig(resources);
        Request request = new Request.Builder()
                .url("http://" + configs[0] + ":" + configs[1] + "/" + configs[2] + "/getComment/" + songId)
                .get()
                .build();
        return new Executor().transfer(request);
    }

    public String addComment(Resources resources, String commentJson) {
        String[] configs = new Executor().getConfig(resources);
        RequestBody requestBody = RequestBody.create(JSON, commentJson);
        Request request = new Request.Builder()
                .url("http://" + configs[0] + ":" + configs[1] + "/" + configs[2] + "/addComment")
                .post(requestBody)
                .build();
        return new Executor().transfer(request);
    }

    public String deleteComment(Resources resources, int commentId) {
        String[] configs = new Executor().getConfig(resources);
        Request request = new Request.Builder()
                .url("http://" + configs[0] + ":" + configs[1] + "/" + configs[2] + "/deleteComment/" + commentId)
                .get()
                .build();
        return new Executor().transfer(request);
    }

    public String getSong(Resources resources, String songName) {
        String[] configs = new Executor().getConfig(resources);
        Request request = new Request.Builder()
                .url("http://" + configs[0] + ":" + configs[1] + "/" + configs[2] + "/getSong/" + songName)
                .get()
                .build();
        return new Executor().transfer(request);
    }

    public String addSongInList(Resources resources, int songId, int slistId) {
        String[] configs = new Executor().getConfig(resources);
        Request request = new Request.Builder()
                .url("http://" + configs[0] + ":" + configs[1] + "/" + configs[2] + "/addSongInList/" + songId + "/" + slistId)
                .get()
                .build();
        return new Executor().transfer(request);
    }

    public String deleteSongInList(Resources resources, int songId, int slistId) {
        String[] configs = new Executor().getConfig(resources);
        Request request = new Request.Builder()
                .url("http://" + configs[0] + ":" + configs[1] + "/" + configs[2] + "/deleteSongInList/" + songId + "/" + slistId)
                .get()
                .build();
        return new Executor().transfer(request);
    }

    public String reco(Resources resources, String filePath) {
        String[] configs = new Executor().getConfig(resources);
        File file = new File(filePath);
        String[] temp = filePath.split("/");
        String filename = temp[temp.length - 1];
        RequestBody fileBody = RequestBody.create(FILE, file);
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addPart(Headers.of("Content-Disposition",
                        "form-data; name=\"recoFile\"; filename=\"" + filename + "\""), fileBody)
                .build();
        Request request = new Request.Builder()
                .url("http://" + configs[0] + ":" + configs[1] + "/" + configs[2] + "/reco")
                .post(requestBody)
                .build();
        return new Executor().transfer(request);
    }

}